'-------------------------------'currently only for single user, processing in bulk request-------------------------------'

ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C1 Invoked Function.vbs",1).ReadAll'调用函数库
'< -----------------End of function-----------------Start Script----------------- >'
ReadUserDetails
winstr = Split(GetC1Env(UserDetail(0)), "cis.jsp", -1, 1)(0) & "uiPage"
LogonEnv : SwitchMenu : validUser(UserDetail(2))
if isErrorIDFound = "true"  then
	msgbox("the  ID doesn't exist or has multiple matched value")
	WScript.quit
end if
'<----------------------------Get Model User  Portals Profile User ID
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
while TabPageContent Is Nothing
	WScript.Sleep 200 : Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
wend
Set ModelUserPPUI = TabPageContent.document.getelementbyid("PORTAL_OVRD_USER")
if ModelUserPPUI.value ="" then
	ModelUserPPUI=""
else
	ModelUserPPUI=ModelUserPPUI.value
end if
'<-----------------------------Change to Characteristics menu'
set fso = createobject("scripting.filesystemobject")
set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0) & "_Sales.txt", true)
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set TabMenu = ie.document.frames(0).document.getelementbyid("tabMenu")
Set TabMenuContent = TabMenu.contentWindow
Set TDList = TabMenuContent.document.getelementsbytagname("td")
SwitchTab("Characteristics")
'<---------------------Locate the Characteristics '
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
Set userCharGridContent = TabPageContent.document.getelementbyid("USER_CHAR_GRID").contentWindow
Set Characteristics= userCharGridContent.document.getelementsbytagname("input")
on error resume next
while Characteristics Is Nothing
	WScript.Sleep 200 : Set Characteristics= userCharGridContent.document.getelementsbytagname("input")
wend
on error goto 0
WScript.Sleep 2000
for each item in Characteristics
	if right(item.id,9) = "$CHAR_VAL" and len(item.value) > 2 and item.value <> "CICLVL12" then
		ReDim Preserve ModelUserSaleList(ModelUserSaleNum)
		ModelUserSaleList(ModelUserSaleNum) = item.value
		file.WriteLine(ModelUserSaleList(ModelUserSaleNum))
		ModelUserSaleNum=ModelUserSaleNum+1
	end if
Next
file.Close : set file = nothing : set fso = nothing
'<---------------------Search end user'
validUser(UserDetail(1))
if isErrorIDFound = "true"  then
	msgbox("the  ID doesn't exist or has multiple matched value")
	WScript.quit
end if
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
if ModelUserSaleNum <> 0 then
	const C_Type = "SALCHN"
	Set userCharGridContent = TabPageContent.document.getelementbyid("USER_CHAR_GRID").contentWindow
'<------------------------Add end user sale channel'
	for num = 0 to UBound(ModelUserSaleList)-1
		charAddID = "IM_USER_CHAR:" & num & "$ADD"
		on error resume next '异常处理'
		Set charAdd =userCharGridContent.document.getelementbyid(charAddID)
		while err.number <> 0 
			Err.Clear : Set charAdd =userCharGridContent.document.getelementbyid(charAddID)
		wend
		on error  goto 0
		charAdd.click : WScript.Sleep 2000
		Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	next
	for num = 0 to UBound(ModelUserSaleList)
		charTypeID ="USER_CHAR:" & num & "$CHAR_TYPE_CD"
		SeqID ="USER_CHAR:" & num & "$SEQ_NUM"
		charValueID ="USER_CHAR:" & num & "$CHAR_VAL"
		Set charType = userCharGridContent.document.getelementbyid(charTypeID)
		Set Seq = userCharGridContent.document.getelementbyid(SeqID)
		Set charValue = userCharGridContent.document.getelementbyid(charValueID)
		
		charType.focus : charType.value=C_Type : charType.FireEvent("onchange")
		WScript.Sleep 1500 : Seq.focus : Seq.value=num+1 : Seq.FireEvent("onblur")
		charValue.focus : charValue.value=ModelUserSaleList(num)  : charValue.FireEvent("onblur") : WScript.Sleep 1500
	next
end if
'<-----------------------------Update End User Portals Profile User ID
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set TabMenu = ie.document.frames(0).document.getelementbyid("tabMenu")
Set TabMenuContent = TabMenu.contentWindow
Set TDList = TabMenuContent.document.getelementsbytagname("td")
SwitchTab("Main")
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
while TabPageContent Is Nothing
	WScript.Sleep 200 : Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
wend
Set EndUserPPUI = TabPageContent.document.getelementbyid("PORTAL_OVRD_USER")
Set UserTypeFlg = TabPageContent.document.getelementbyid("USER_TYPE_FLG")
ie.Visible=true
if ModelUserPPUI <> "" and ModelUserPPUI <> "PORTAL" then
	UserTypeFlg.value="TMPL" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500
	UserTypeFlg.value="" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500	
	EndUserPPUI.value=ModelUserPPUI: EndUserPPUI.FireEvent("onblur") : WScript.Sleep 1500
	ie.document.frames(0).document.getelementbyid("IM_SAVE").click
elseif EndUserPPUI.value = "PORTAL" then
	UserTypeFlg.value="TMPL" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500
	UserTypeFlg.value="" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500
	REM EndUserPPUI.value="" : EndUserPPUI.FireEvent("onblur") : WScript.Sleep 1500
	ie.document.frames(0).document.getelementbyid("IM_SAVE").click
end if
ie.document.frames(0).document.getelementbyid("IM_SAVE").click
msgbox "Script finished successfully!"
